package com.xywl.escort.view.guide

import android.os.Bundle
import android.support.v4.view.ViewPager
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.xywl.escort.R
import com.xywl.escort.adapter.GuidePagerAdapter
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.common.PreferenceKeys
import com.xywl.escort.utils.RxSPTool
import kotlinx.android.synthetic.main.activity_guide.*

@Route(path = "/xy/guide")
class GuideActivity : BaseActivity() {
    private var mViewList: ArrayList<View>? = null
    private var mOneDot: ImageView? = null
    private var mTwoDot: ImageView? = null
    private var mThreeDot: ImageView? = null
    private var mDistance: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_guide)

        initData()

        viewpager.adapter = GuidePagerAdapter(mViewList)
        addDots()
        moveDots()
        viewpager.setPageTransformer(true, DepthPageTransformer())

        bt_next.setOnClickListener {
            RxSPTool.putBoolean(this, PreferenceKeys.IS_FIRST, true)
            ARouter.getInstance().build("/xy/main").navigation()
            finish()
        }
    }

    private fun initData() {
        mViewList = ArrayList()
        val view1 = layoutInflater.inflate(R.layout.guide_indicator1, null)
        val view2 = layoutInflater.inflate(R.layout.guide_indicator2, null)
        val view3 = layoutInflater.inflate(R.layout.guide_indicator3, null)
        mViewList!!.add(view1)
        mViewList!!.add(view2)
        mViewList!!.add(view3)
    }

    private fun addDots() {
        mOneDot = ImageView(this)
        mOneDot!!.setImageResource(R.mipmap.home_banner2)
        val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams.setMargins(0, 0, 40, 0)
        layout_dot.addView(mOneDot, layoutParams)
        mTwoDot = ImageView(this)
        mTwoDot!!.setImageResource(R.mipmap.home_banner2)
        layout_dot.addView(mTwoDot, layoutParams)
        mThreeDot = ImageView(this)
        mThreeDot!!.setImageResource(R.mipmap.home_banner2)
        layout_dot.addView(mThreeDot, layoutParams)
        setClickListener()
    }

    private fun setClickListener() {
        mOneDot!!.setOnClickListener { viewpager.currentItem = 0 }
        mTwoDot!!.setOnClickListener { viewpager.currentItem = 1 }
        mThreeDot!!.setOnClickListener { viewpager.currentItem = 2 }
    }

    private fun moveDots() {
        iv_light_dots.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                //获得两个圆点之间的距离
                mDistance = layout_dot.getChildAt(1).left - layout_dot.getChildAt(0).left
                iv_light_dots.viewTreeObserver.removeGlobalOnLayoutListener(this)
            }
        })
        viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                //页面滚动时小白点移动的距离，并通过setLayoutParams(params)不断更新其位置
                val leftMargin = mDistance * (position + positionOffset)
                val params = iv_light_dots.layoutParams as RelativeLayout.LayoutParams
                params.leftMargin = leftMargin.toInt()
                iv_light_dots.layoutParams = params
                if (position == 2) {
                    bt_next.visibility = View.VISIBLE
                }
                if (position != 2 && bt_next.visibility == View.VISIBLE) {
                    bt_next.visibility = View.GONE
                }
            }

            override fun onPageSelected(position: Int) {
                //页面跳转时，设置小圆点的margin
                val leftMargin = mDistance * position
                val params = iv_light_dots.layoutParams as RelativeLayout.LayoutParams
                params.leftMargin = leftMargin
                iv_light_dots.layoutParams = params
                if (position == 2) {
                    bt_next.visibility = View.VISIBLE
                }
                if (position != 2 && bt_next.visibility == View.VISIBLE) {
                    bt_next.visibility = View.GONE
                }
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })
    }
}
